/*==========================run payRoll 相关接口==============================*/
import Ajax from './apiAjax';
export  default {
  state:{
    scheduleList:null,
    schedulePeriod:null,
    schedulePersons:{
      list:null,
      pb:null
    },
    experiencePayRoll:null,//试算工资数据
    yearPayCost: null,
    calculatePersonPay:{},
    calculatePayDetail:null,
    previewPays:{
      pb:null,
      list:null
    },
    previewPayCost:null,
    submitPayRoll:{
      pb:null,
      list:null,
      obj:null
    },
  },
  mutations:{
    setCommonStateData(state,{key,data}){
      state[key] = data;
    },
    setCalculatePersonPay(state,data){
      var copyData = JSON.parse(JSON.stringify(state.calculatePersonPay));
      copyData['person_'+data.personId] = data;
      state.calculatePersonPay = copyData;
    },
  },
  actions:{
    /**
     *查询发薪日程列表(url:pay/queryPayScheduleList)
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    getPayScheduleList({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/queryPayScheduleList',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'scheduleList',data:data.data.scheduleList});
            resolve(data);
          }
        })
      })
    },
    /**
     * 查询发薪日程的周期列表
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    getPaySchedulePeriod({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/querySchedulePeriod',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'schedulePeriod',data:data.data});
            resolve(data);
          }
        })
      })
    },
    /**
     *查询发薪日程的周期的员工
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    getPaySchedulePerson({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/querySchedulePerson',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'schedulePersons',data:{list:data.data.personList,pb:data.pb}});
            resolve(data);
          }
        })
      })
    },
    /**
     * 试算工资
     * @param commit
     * @param paramData  请求参数：{"personId":"9797","hour":"7",//单位：小时"commision":"890"//单位：美元}
     * @returns {Promise}
     */
    getExperiencePayRoll({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/expriencePayRoll',
          data:JSON.stringify(paramData),
          contentType: "application/json;charset=UTF-8",
          success:function (data) {
            commit('setCommonStateData',{key:'experiencePayRoll',data:data.data});
            resolve(data);
          }
        })
      })
    },
    getYearPayCost({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/getYearTotalCost',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'yearPayCost',data:data.data});
            resolve(data);
          }
        })
      })
    },
    getCalculatePersonPay({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/calculateGrossyPay',
          data:JSON.stringify(paramData),
          contentType: "application/json;charset=UTF-8",
          success:function (data) {
            commit('setCalculatePersonPay',data.data);
            resolve(data);
          }
        })
      })
    },
    getCalculatePayDetail({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/calculatePayDetail',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'calculatePayDetail',data:data.data});
            resolve(data);
          }
        })
      })
    },
    /**
     * 预览工资列表
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    getPreviewPayList({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/previewPayList',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'previewPays',data:{pb:data.pb,list:data.data.payList}});
            resolve(data);
          }
        })
      })
    },
    getPreviewPayCost({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/previewPayStatisc',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'previewPayCost',data:data.data});
            resolve(data);
          }
        })
      })
    },
    getSubmitPayRoll({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/pay/submitPayRoll',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'submitPayRoll',data:{pb:data.pb,list:data.data.payList,obj:data.data}});
            resolve(data);
          }
        })
      })
    },
  },
  getters: {
    scheduleList: (state, getters) => {
      return state.scheduleList;
    },
    schedulePeriod: (state, getters)=> {
      return state.schedulePeriod;
    },
    schedulePersonList: (state, getters)=> {
      return state.schedulePersons.list;
    },
    schedulePersonListModel: (state, getters)=> {
      var data = state.schedulePersons.list;
      var obj={};
      if(data && data.length){
        for(var i in data){
          obj['person_'+data[i].uuid] = {
            "personId": data[i].uuid,
            "regularH": "",//单位":小时
            "overTimeH": "",//单位":小时
            "doubleOverTimeH": "",//单位":小时
            "sickH": "",//单位":小时
            "vocationH": "",//单位":小时
            "holidayH": "",//单位":小时
            "bonusAmount": "",//单位":$
            "commisionAmount": "", //单位":$
          }
        }
      }
      return obj;
    },
    experiencePayRoll:(state, getters)=> {
      return state.experiencePayRoll;
    },
    yearPayCost: (state, getters) => {
      return state.yearPayCost;
    },
    yearPayCostObj: (state, getters) => {
      var obj;
      if (state.yearPayCost) {
        let data = state.yearPayCost;
        obj = {
          payList: [
            {name: 'EMPLOYER', value: data.employerCost, rate: data.employerCostRate},
            {name: 'NET PAY', value: data.netPay, rate: data.netPayRate},
            {name: 'EMPLOYEE', value: data.employeeCost, rate: data.employeeCostRate},
          ],
          payUnit: data.unit,
          payYear: data.year,
          payTotal: data.totalCost
        }
      }
      return obj;
    },
    previewPayCostObj: (state, getters) => {
      var obj;
      if (state.previewPayCost) {
        let data = state.previewPayCost;
        obj = {
          payList: [
            {name: 'EMPLOYER', value: data.employerCost, rate: data.employerCostRate},
            {name: 'NET PAY', value: data.netPay, rate: data.netPayRate},
            {name: 'EMPLOYEE', value: data.employeeCost, rate: data.employeeCostRate},
          ],
          payUnit: data.unit,
          scheduleName: data.scheduleName,
          periodStart:data.periodStart,
          periodEnd:data.periodEnd,
          payDate:data.payDate,
          payCheckCount:data.payCheckCount,
          payTotal: data.costTotal
        }
      }
      return obj;
    },
    calculatePersonPay: (state, getters) => {
      return state.calculatePersonPay;
    },
    calculatePayDetail: (state, getters) => {
      return state.calculatePayDetail;
    },
    previewPayList: (state, getters) => {
      return state.previewPays.list;
    },
    previewPayListPb: (state, getters) => {
      return state.previewPays.pb;
    },
    submitPayRollList: (state, getters) => {
      return state.submitPayRoll.list;
    },
    submitPayRollListPb: (state, getters) => {
      return state.submitPayRoll.pb;
    },
    submitPayRoll: (state, getters) => {
      return state.submitPayRoll.obj;
    },


  }
}
